home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp 2.0.1 (Many Libraries) / Interfaces / CIncludes / UPatch.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-25  |  5.1 KB  |  114 lines  |  [TEXT/MPS ]

  1. /*[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n-]*/
  2. /* UPatch.p */
  3. /* Copyright © 1985-1990 by Apple Computer, Inc.  All rights reserved. */
  4. /*[f-]*/
  5. /*
  6.         T H E O R Y   O F   O P E R A T I O N
  7. This unit implements a general trap-patching mechanism.  Two types of
  8. patches are implemented:
  9. Normal Patch    The trap is patched such that it jumps to the routine
  10.             of your choice, ignoring the routine it previously
  11.             jumped to.  On a 128K ROM machine, the patch is made
  12.             by setting the trap address to your routine.  On a 64K
  13.             machine, a small block of code is allocated in the
  14.             system heap:  The trap address is set to point to the
  15.             block, and the block contains code to jump to your
  16.             routine.
  17. Head Patch  The trap is patched such that it jumps to the
  18.             routine of your choice, then jumps to the trap's
  19.             routine.  Head patches come in two flavors:  with
  20.             no parameters and with one long-word parameter.  The
  21.             patch is made by allocating a small block of code in
  22.             the application heap (system heap for 64K ROMs).  The
  23.             trap address is set to the block, and the block contains
  24.             code to jump to your routine and return to the trap's
  25.             original address.
  26. References to patches are kept in TrapPatch records.  For each trap
  27. patched you must define a variable of type TrapPatch.   This unit links
  28. the TrapPatch records to form a linked list for facilitate unpatching
  29. all patches without specifying each individual patch.
  30. */
  31. /*[f+]*/
  32. #ifndef  __UPatch__
  33. #define __UPatch__  0
  34. #endif
  35. #if  ! __UPatch__
  36. #define __UPatch__  1
  37.  
  38.         /* • Auto-Include the requirements for this unit's interface. */
  39. #ifndef  __TYPES__
  40. #include "Types.h"
  41. #endif
  42. #ifndef  __MEMORY__
  43. #include "Memory.h"
  44. #endif
  45.  
  46.                                                         /* Preferred. The pointer type _MUST_ be
  47.                                                          declared first since the record is self
  48.                                                          referential */
  49. typedef struct TrapPatch *TrapPatchPtr; 
  50. struct TrapPatch {
  51.     Ptr jmpPtr;                                            /* Addr of sys heap block */
  52.     short trapNum;                                        /* trap # being patched */
  53.     long oldTrapAddr;                                    /* old trap address */
  54.     TrapPatchPtr nextPatch;                                /* next link in linked list of patches */
  55. };
  56.  
  57. struct CallBack {
  58.     short saveRtnAdd;                                    /* Internal use */
  59.     short moveRefCon;                                    /* Internal use */
  60.     long refCon;                                        /* the refCon that will be passed as last
  61.                                                          parm */
  62.     short targOffset;                                    /* Internal use */
  63.     short jmpInst;                                        /* Internal use */
  64.     long jmpTarg;                                        /* Internal use */
  65. };
  66. typedef CallBack *CallBackPtr;
  67.  
  68.         /* The following variable is really private but is in the interface just in case
  69.         you're doing something really wierd and you really need it */
  70.  
  71. extern pascal TrapPatchPtr pPatchList;                    /* Head of linked list of patches */
  72.  
  73. extern pascal void InitUPatch(void);
  74.         /* Call this once to set things up. Initializes the linked list of patches to NIL. */
  75.  
  76. extern pascal void EachPatchDo(pascal Boolean (*DoToPatch)(TrapPatchPtr thePatchPtr, void *
  77.    DoToPatch_StaticLink), void *DoToPatch_StaticLink);
  78.         /* Calls DoToPatch for each patch in the list from pPatchList to the last patch.
  79.         Stops at end of list or when DoToPatch returns TRUE.  DoToPatch may not remove patches. */
  80.  
  81. extern pascal short HeadPatch(TrapPatch *thePatch, short theTrapNum, Ptr theRoutine);
  82.         /* HeadPatch patches a trap such that it calls theRoutine first, then executes the old trap
  83.         routine. theRoutine must be a procedure with no arguments. This type of patch is
  84.         implemented by setting up a block in the system heap, patching the trap to jump to the
  85.         block, and inserting code in the block that pushes the original trap address on the stack
  86.         and jumps to theRoutine. Thus, theRoutine returns to the original trap routine. */
  87.  
  88. extern pascal short Head1Patch(TrapPatch *thePatch, short theTrapNum, Ptr theRoutine);
  89.         /* Head1Patch is just like HeadPatch, except that it is intended to patch traps with a s
  90.            ingle
  91.         long-word parameter. The parameter passed to the trap is also passed to theRoutine.
  92.         theRoutine must be a procedure with one long-word parameter. */
  93.  
  94. extern pascal short PatchTrap(TrapPatch *thePatch, short theTrapNum, Ptr theRoutine);
  95.         /* PatchTrap patches the given trap to theRoutine.  On a 64K ROM machine, we must set up a
  96.         block in the system heap, patch the trap to jump to the block, and insert code in the block
  97.         to jump to theRoutine. */
  98.  
  99. extern pascal void SetCallBack(Ptr targProc, long itsRefCon, CallBackPtr theCallBackPtr);
  100.         /* Prepares a call back record for use.  Stuffs in the target of the call back and the r
  101.            efcon
  102.         to pass as an _additional_ and _last_ parameter to it.  Useful for getting conte
  103.    xt when being
  104.         called back from the toolBox.  Also useful for avoiding use of globals when gett
  105.    ing context */
  106.  
  107. extern pascal void UnpatchTrap(TrapPatch *thePatch);
  108.         /* UnpatchTrap unpatches the given trap. */
  109.  
  110. extern pascal void UnpatchAll(void);
  111.         /* UnpatchAll unpatches all traps. */
  112. #endif
  113.  
  114.